/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.maven.nodetype;

import offset.nodes.Constants;

/**
 *
 * @author Walter L�tgenau
 */
public class Name {
    public static final String PACKAGE_DELIMITER = ".";

    String name;
    String prefix;
    String localName;

    public Name(String name) {
        this.name = name;
        String nameParts[] = name.split(Constants.NAMESPACE_DELIMITER);
        prefix = nameParts[0];
        localName = nameParts[1];
    }

    public String getLocalName() {
        return localName;
    }

    public String getName() {
        return name;
    }

    public String getPrefix() {
        return prefix;
    }

    public String nameToJavaClassName() {
        return localName.substring(0, 1).toUpperCase() + localName.substring(1);
    }

    public String nameToJavaPackageAndClassName() {
        return NodeTypeMojo.GENERATED_PACKAGE + PACKAGE_DELIMITER + getPrefix() + PACKAGE_DELIMITER + nameToJavaClassName();
    }

    public String nameToJavaVariableName() {
        return "_" + localName.substring(0, 1).toLowerCase() + localName.substring(1);
    }

    public String nameToConstant() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < localName.length(); i++) {
            char c = localName.charAt(i);
            if (Character.isUpperCase(c))
                builder.append("_");
            builder.append(Character.toUpperCase(c));
        }

        return builder.toString();
    }

    public static String propertyTypeToJava(String type) {
        String result = "String";

        if (type.equals("BOOLEAN"))
            result = "boolean";
        else if (type.equals("STRING")
                || type.equals("NAME")
                || type.equals("PATH")
                || type.equals("REFERENCE")
                || type.equals("UNDEFINED"))
            result = "String";
        else if (type.equals("DATE"))
            result = "Calendar";
        else if (type.equals("DOUBLE"))
            result = "double";
        else if (type.equals("LONG"))
            result = "long";

        return result;
    }
}
